//
//  ICMRepairPartSearchTableViewCell.m
//  ICrEAM
//
//  Created by FoREVer_SaD on 16/11/2.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMRepairPartSearchTableViewCell.h"

@interface ICMRepairPartSearchTableViewCell ()

@property (nonatomic, strong) UILabel *quantityLabel;

@property (nonatomic, strong) UILabel *partNameLabel;

@property (nonatomic, strong) UILabel *partOrderLabel;

@property (nonatomic, strong) UILabel *partPropertyLabel;

@property (nonatomic, strong) UILabel *stockPlaceLabel;

@property (nonatomic, strong) UILabel *stockNumberLabel;

@property (nonatomic, strong) UILabel *priceLabel;

@property (nonatomic, strong) UIButton *leftButton;

@property (nonatomic, strong) UIView *seventhPartSep;

@property (nonatomic, strong) UIButton *reduceBtn;

@property (nonatomic, strong) UIButton *increaseBtn;

@end

@implementation ICMRepairPartSearchTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.quantity = 0;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        UIView *repairPartCellView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ActualWidth(924), ActualHeight(95))];
        repairPartCellView.backgroundColor = BackgroundColor_Gray;
        [self.contentView addSubview:repairPartCellView];
        
        UIButton *eventOperateButton = [UIButton buttonWithType:UIButtonTypeCustom];
        eventOperateButton.frame = CGRectMake(0, 0, ActualWidth(96), ActualHeight(95));
        [eventOperateButton setImage:[UIImage imageNamed:@"btn_activity_notchoose"] forState:UIControlStateNormal];
        [eventOperateButton setImage:[UIImage imageNamed:Appointment_ActivityChoose] forState:UIControlStateSelected];
        eventOperateButton.imageEdgeInsets = UIEdgeInsetsMake(ActualHeight(31), ActualWidth(31.5), ActualHeight(31), ActualWidth(31.5));
        [eventOperateButton addTarget:self action:@selector(clickOperationButton:) forControlEvents:UIControlEventTouchUpInside];
        [repairPartCellView addSubview:eventOperateButton];
        self.eventOperateButton = eventOperateButton;
        
//        UIView *blankView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ActualWidth(96), ActualHeight(95))];
//        blankView.backgroundColor = [UIColor clearColor];
//        [repairPartCellView addSubview:blankView];
        
        UIView *firstPartSep = [[UIView alloc] initWithFrame:CGRectMake(ActualWidth(96), 0, 1, repairPartCellView.height)];
        firstPartSep.backgroundColor = [UIColor whiteColor];
        [repairPartCellView addSubview:firstPartSep];
        
        UILabel *partNameLabel = [self createLabelWithText:@"" frame:CGRectMake(firstPartSep.rightEdge, ActualHeight(12), ActualWidth(150), ActualHeight(44))];
        partNameLabel.font = BoldNaviFont;
        UILabel *partOrderLabel = [self createLabelWithText:@"" frame:CGRectMake(firstPartSep.rightEdge, partNameLabel.bottomEdge+ActualHeight(10), ActualWidth(150), ActualHeight(20))];
        partOrderLabel.textColor = ContentTextColor_Gray;
        self.partNameLabel = partNameLabel;
        self.partOrderLabel = partOrderLabel;
        
        [repairPartCellView addSubview:partNameLabel];
        [repairPartCellView addSubview:partOrderLabel];
        
        UIView *secondPartSep = [[UIView alloc] initWithFrame:CGRectMake(partNameLabel.rightEdge, 0, 1, repairPartCellView.height)];
        secondPartSep.backgroundColor = [UIColor whiteColor];
        [repairPartCellView addSubview:secondPartSep];
        
        UILabel *partPropertyLabel = [self createLabelWithText:@"" frame:CGRectMake(secondPartSep.rightEdge, ActualHeight(37), ActualWidth(105), ActualHeight(16))];
        [repairPartCellView addSubview:partPropertyLabel];
        self.partPropertyLabel = partPropertyLabel;
        
        UIView *thirdPartSep = [[UIView alloc] initWithFrame:CGRectMake(partPropertyLabel.rightEdge, 0, 1, repairPartCellView.height)];
        thirdPartSep.backgroundColor = [UIColor whiteColor];
        [repairPartCellView addSubview:thirdPartSep];
        
        UILabel *stockPlaceLabel = [self createLabelWithText:@"" frame:CGRectMake(thirdPartSep.rightEdge, partPropertyLabel.y, ActualWidth(150), ActualHeight(16))];
        [repairPartCellView addSubview:stockPlaceLabel];
        self.stockPlaceLabel = stockPlaceLabel;
        
        UIView *fourthPartSep = [[UIView alloc] initWithFrame:CGRectMake(stockPlaceLabel.rightEdge, 0, 1, repairPartCellView.height)];
        fourthPartSep.backgroundColor = [UIColor whiteColor];
        [repairPartCellView addSubview:fourthPartSep];
        
        UILabel *stockNumberLabel = [self createLabelWithText:@"" frame:CGRectMake(fourthPartSep.rightEdge, stockPlaceLabel.y, ActualWidth(90), ActualHeight(16))];
        [repairPartCellView addSubview:stockNumberLabel];
        self.stockNumberLabel = stockNumberLabel;
        
        UIView *fifthPartSep = [[UIView alloc] initWithFrame:CGRectMake(stockNumberLabel.rightEdge, 0, 1, repairPartCellView.height)];
        fifthPartSep.backgroundColor = [UIColor whiteColor];
        [repairPartCellView addSubview:fifthPartSep];
        
        UILabel *priceLabel = [self createLabelWithText:@"0.00" frame:CGRectMake(fifthPartSep.rightEdge, stockNumberLabel.y, ActualWidth(96), ActualHeight(16))];
        [repairPartCellView addSubview:priceLabel];
        self.priceLabel = priceLabel;
        
        UIView *sixthPartSep = [[UIView alloc] initWithFrame:CGRectMake(priceLabel.rightEdge, 0, 1, repairPartCellView.height)];
        sixthPartSep.backgroundColor = [UIColor whiteColor];
        [repairPartCellView addSubview:sixthPartSep];
        
        UILabel *quantityLabel = [self createLabelWithText:[@(self.quantity) stringValue] frame:CGRectMake(sixthPartSep.rightEdge, priceLabel.y, ActualWidth(110), ActualHeight(16))];
        [repairPartCellView addSubview:quantityLabel];
        self.quantityLabel = quantityLabel;
        
        UIButton *reduceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [reduceBtn setTitle:@"-" forState:UIControlStateNormal];
        [reduceBtn setTitleColor:ContentTextColor_Black forState:UIControlStateNormal];
        reduceBtn.titleLabel.font = TitleFont;
        reduceBtn.frame = CGRectMake(quantityLabel.centerX-ActualWidth(44), ActualHeight(33), ActualWidth(22), ActualWidth(22));
        [reduceBtn addTarget:self action:@selector(reduceQuantity) forControlEvents:UIControlEventTouchUpInside];
        [repairPartCellView addSubview:reduceBtn];
        self.reduceBtn = reduceBtn;

        
        UIButton *increaseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [increaseBtn setTitle:@"+" forState:UIControlStateNormal];
        [increaseBtn setTitleColor:ContentTextColor_Black forState:UIControlStateNormal];
        increaseBtn.titleLabel.font = TitleFont;
        increaseBtn.frame = CGRectMake(quantityLabel.centerX+ActualWidth(22), ActualHeight(33), ActualWidth(22), ActualWidth(22));
        [increaseBtn addTarget:self action:@selector(increaseQuantity) forControlEvents:UIControlEventTouchUpInside];
        [repairPartCellView addSubview:increaseBtn];
        self.increaseBtn = increaseBtn;
        
        UIView *seventhPartSep = [[UIView alloc] initWithFrame:CGRectMake(quantityLabel.rightEdge, 0, 1, repairPartCellView.height)];
        seventhPartSep.backgroundColor = [UIColor whiteColor];
        [repairPartCellView addSubview:seventhPartSep];
        self.seventhPartSep = seventhPartSep;
        
        UIButton *leftButton =  [[UIButton alloc]initWithFrame:CGRectMake(seventhPartSep.rightEdge+ActualWidth(25), ActualHeight(26), repairPartCellView.width-seventhPartSep.rightEdge-ActualWidth(50), ActualHeight(35))];
        [leftButton setUpButtonWithNormolTitle:@"留存" selectedTitle:@"留存" normolTitleSize:ActualWidth(15) selectedTitleSize:ActualWidth(15) normolTitleColor:ContentTextColor_Gray selectedTitleColor:[UIColor whiteColor] normolBcakImage:nil selectedBackImage:nil actionSEL:@selector(clickLeftButton:) target:self];

        leftButton.layer.borderColor = [TitleColor CGColor];
        leftButton.layer.borderWidth = 1;

        [repairPartCellView addSubview:leftButton];
        self.leftButton = leftButton;
        
        UIView *seperator = [[UIView alloc] initWithFrame:CGRectMake(0, 0, repairPartCellView.width, 1.0)];
        seperator.backgroundColor = [UIColor whiteColor];
        [self addSubview:seperator];
    }
    return self;
}


- (void)reduceQuantity{
    if (self.quantity > 0) {
        self.quantity--;
        self.model.OBLIGATED_QUANTITY = [NSString stringWithFormat:@"%.2f", (float)self.quantity];
        self.quantityLabel.text = [@(self.quantity) stringValue];
        if (self.quantity == 0) {
            if ([self.delegate respondsToSelector:@selector(repairPartSearchTableViewCell:andQuantity:)]) {
                [self.delegate repairPartSearchTableViewCell:self andQuantity:self.quantity];
            }
        }
    }
}

- (void)increaseQuantity{
    self.quantity++;
    self.model.OBLIGATED_QUANTITY = [NSString stringWithFormat:@"%.2f", (float)self.quantity];
    self.quantityLabel.text = [@(self.quantity) stringValue];
    float quantity = [self.model.REAL_QUANTITY floatValue];

    if (quantity<[self.model.OBLIGATED_QUANTITY floatValue]){
        if (self.leftButton.selected) {
            self.leftButton.selected = NO;
            [self setupButtonStates:self.leftButton];
            if (self.eventOperateButton.selected&&[self.delegate respondsToSelector:@selector(quantityNotEnough:)]) {
                [self.delegate quantityNotEnough:@"零件数量不足,不能留存!添加的配件数量大于可用库存，将引起负库存。"];
            }
        }else{
            if (self.eventOperateButton.selected) {
                [self.delegate quantityNotEnough:@"添加的配件数量大于可用库存，将引起负库存。"];
            }
        }
        
    }
}

- (void)clickLeftButton:(UIButton *)sender {
    if (sender.selected) {
        sender.selected = NO;
    }else{
        if ([self.model.REAL_QUANTITY floatValue]>=[self.model.OBLIGATED_QUANTITY floatValue]) {
            sender.selected = YES;
        }else{
            if ([self.delegate respondsToSelector:@selector(quantityNotEnough:)]) {
                [self.delegate quantityNotEnough:@"零件数量不足,不能留存!"];
            }
        }
    }
    [self setupButtonStates:sender];
    //@"12781001"：是 @"12781002"：否
    
}

- (void)setupButtonStates:(UIButton*)sender{
    if (sender.selected) {
        sender.backgroundColor = TitleColor;
        sender.layer.borderColor = [TitleColor CGColor];
        sender.layer.borderWidth = 1;
    } else {
        sender.backgroundColor = [UIColor whiteColor];
        sender.layer.borderColor = SepColor_gray.CGColor;
        sender.layer.borderWidth = 1;
    }
    self.model.IS_OBLIGATED = sender.selected?ICMEnumIntToString(ICMEnumYESOrNOYES):ICMEnumIntToString(ICMEnumYESOrNONO);
}

- (void)setupLabelsWithModel:(ICMRepairPartsModel *)model{
    self.model = model;
    self.partNameLabel.text = model.PART_NAME;
    self.partOrderLabel.text = model.PART_NO;
    self.partPropertyLabel.text = [model.OEM_TAG isEqualToString:ICMEnumIntToString(ICMEnumYESOrNOYES)]?@"原装零件":@"非原装零件";
//    NSArray *reasonArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumPartsType)];
//    if([model.OEM_TAG isEqualToString:@"12781001"]){
//    
//    
//    }
//    for (ParameterModel *parameterModel in reasonArray) {
//        if([model.OEM_TAG isEqualToString:@"12781001"]){
//            self.partPropertyLabel.text = [ICrEAMTool getDeatilCodeWithParameterDesc:<#(NSString *)#> withCode:<#(NSString *)#>];
//        }
//    }

    self.priceLabel.text = model.SALES_PRICE;
    self.stockPlaceLabel.text = model.STORAGE_POSITION_CODE;
    self.stockNumberLabel.text = model.REAL_QUANTITY;

    self.quantityLabel.text = model.OBLIGATED_QUANTITY;
    
    self.quantity = [self.quantityLabel.text floatValue];
    
    if (self.hasPartLeft) {
        self.seventhPartSep.hidden = NO;
        self.leftButton.hidden = NO;
        self.quantityLabel.frame = CGRectMake(self.quantityLabel.x, self.quantityLabel.y, ActualWidth(110), self.quantityLabel.height);
        self.leftButton.selected = [self.model.IS_OBLIGATED isEqualToString:ICMEnumIntToString(ICMEnumYESOrNOYES)];
        self.leftButton.backgroundColor = self.leftButton.selected?TitleColor:[UIColor whiteColor];
        self.leftButton.layer.borderColor = self.leftButton.selected?[TitleColor CGColor]:SepColor_gray.CGColor;
        self.leftButton.layer.borderWidth = 1;
        if ([model.REAL_QUANTITY floatValue] >= [model.OBLIGATED_QUANTITY floatValue]) {
            model.IS_OBLIGATED = model.IS_OBLIGATED?:ICMEnumIntToString(ICMEnumYESOrNOYES);
            self.leftButton.enabled = YES;
        }else if ([model.REAL_QUANTITY floatValue] <= [model.OBLIGATED_QUANTITY floatValue]){
            model.IS_OBLIGATED = ICMEnumIntToString(ICMEnumYESOrNONO);
            self.leftButton.enabled = NO;
        }
        if ([model.IS_OBLIGATED isEqualToString:ICMEnumIntToString(ICMEnumYESOrNOYES)]) {
            self.leftButton.selected = YES;
            self.leftButton.backgroundColor = TitleColor;
            self.leftButton.layer.borderColor = [TitleColor CGColor];
            self.leftButton.layer.borderWidth = 1;
        } else {
            self.leftButton.selected = NO;
            self.leftButton.backgroundColor = [UIColor whiteColor];
            self.leftButton.layer.borderColor = SepColor_gray.CGColor;
            self.leftButton.layer.borderWidth = 1;
        }
        
    } else {
        self.seventhPartSep.hidden = YES;
        self.leftButton.hidden = YES;
        self.quantityLabel.frame = CGRectMake(self.quantityLabel.x, self.quantityLabel.y, (ActualWidth(924)-self.priceLabel.rightEdge-1), self.quantityLabel.height);
    }
    self.reduceBtn.frame = CGRectMake(self.quantityLabel.centerX-ActualWidth(44), ActualHeight(33), ActualWidth(22), ActualWidth(22));
    self.increaseBtn.frame = CGRectMake(self.quantityLabel.centerX+ActualWidth(22), ActualHeight(33), ActualWidth(22), ActualWidth(22));
}

- (UILabel *)createLabelWithText:(NSString *)text frame:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = TitleFont;
    label.textColor = ContentTextColor_Black;
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}

- (void)clickOperationButton:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(selectPartWithCell:)]) {
        [self.delegate selectPartWithCell:self];
    }
}

@end
